package com.good.msgsys.controller;

import com.good.msgsys.dto.Result;
import com.good.msgsys.dto.ResultCode;
import com.good.msgsys.entity.Opus;
import com.good.msgsys.service.OpusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author junlong
 * @date 2020/12/31 10:18
 */
@Api(tags = "著作模块")
@RestController
@RequestMapping("/api/opus")
public class OpusController {
    @Autowired
    private OpusService opusService;

    // 新增著作
    @ApiOperation("新增著作")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public Result<Opus> add(@ApiParam("著作实体") @RequestBody Opus opus) {
        opusService.add(opus);
        return new Result<>(ResultCode.SUCCESS, opus);
    }

    // 根据id删除著作
    @ApiOperation("根据id删除著作")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result<Opus> deleteById(@ApiParam("著作id") @PathVariable("id") String id){
        opusService.deleteById(id);
        return new Result<>(ResultCode.SUCCESS);
    }

    // 更新著作
    @ApiOperation("更新著作")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result<Opus> update(@ApiParam("著作id") @PathVariable("id") String id, @ApiParam("著作实体") @RequestBody Opus opus){
        opus.setId(id);
        opusService.update(opus);
        return new Result<>(ResultCode.SUCCESS);
    }

    // 根据id查询著作
    @ApiOperation("根据id查询著作")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result<Opus> findById(@ApiParam("著作id") @PathVariable("id") String id) {
        Opus opus = opusService.findById(id);
        return new Result<>(ResultCode.SUCCESS, opus);
    }

    // 查询所有著作
    @ApiOperation("查询所有著作")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Result<List<Opus>> findAll() {
        List<Opus> opuses = opusService.findAll();
        return new Result<>(ResultCode.SUCCESS, opuses);
    }
}
